//
//  AXHomeVC.m
//  AnXin100
//
//  Created by 天质弘耕 on 2018/10/22.
//  Copyright © 2018年 tzhg. All rights reserved.
//

#import "AXHomeVC.h"
#import "XXPageTabView.h"
#import "XXPageTabItemLable.h"
#import "Header.h"
#import "ZXCategories.h"
#import "AXHomeNcpscVC.h"
#import "AXHomeNzscVC.h"
#import "AXHomeZxktVC.h"
#import "AXHomeNtsqVC.h"
#import "AXHomeZjzdVC.h"
#import "TLCityPickerController.h"
#import "LLSearchViewController.h"
#import "UIViewController+HBD.h"
#import "NetWorkHelp.h"
  
@interface AXHomeVC ()<XXPageTabViewDelegate>

@property (nonatomic, strong) XXPageTabView *pageTabView;

@end

@implementation AXHomeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.pageTabView];
    [self setupNavigationItems];
     self.hbd_barShadowHidden = YES;
    
//    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
//    [NetWorkHelp yl_postDataWith:nil url:@"api/knowledgeCrop/queryCftLevelOneList.do" success:^(id respose) {
//        
//        NSLog(@"%@", respose);
//        
//    } failure:^(NSError *error) {
//        NSLog(@"%@", error);
//
//    }];
}

- (void)setupNavigationItems {
    
    /** 导航条左边的按钮 */
    UIButton *dwBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
    [dwBtn addTarget:self action:@selector(dwAction:) forControlEvents:UIControlEventTouchUpInside];
    dwBtn.layer.cornerRadius = 5;
    dwBtn.backgroundColor = [UIColor colorWithHex:0xFFF1F1F1];
    //[dwBtn setTitleColor:[UIColor colorWithHex:0x29b566] forState:UIControlStateNormal];
    [dwBtn setImage:[UIImage imageNamed:@"navdw"] forState:UIControlStateNormal];
    //创建富文本
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:@"昆明"];
    [attri addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHex:0x29b566] range:NSMakeRange(0, 2)];
    [attri addAttribute:NSFontAttributeName
                  value:[UIFont systemFontOfSize:12.0f]
                  range:NSMakeRange(0, 2)];
    //NSTextAttachment可以将要插入的图片作为特殊字符处理
    NSTextAttachment *attch = [[NSTextAttachment alloc] init];
    //定义图片内容及位置和大小
    attch.image = [UIImage imageNamed:@"spread"];
    attch.bounds = CGRectMake(0, -2, 10, 10);
    //创建带有图片的富文本
    NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:attch];
    //将图片放在最后一位
    [attri appendAttributedString:string];
    [dwBtn setAttributedTitle:attri forState:UIControlStateNormal];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:dwBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    //客服按钮
    UIButton *kfBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    kfBtn.layer.cornerRadius = 5;
    kfBtn.backgroundColor = [UIColor colorWithHex:0xFFF1F1F1];
    [kfBtn setImage:[UIImage imageNamed:@"navkf"] forState:UIControlStateNormal];
    kfBtn.frame = CGRectMake(0, 0, 60, 30);
    [kfBtn setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
    [kfBtn addTarget:self action:@selector(kfAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:kfBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    /** 搜索 */
  
     UIButton *searchButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-140, 30)];
    searchButton.backgroundColor = [UIColor colorWithHex:0xFFF1F1F1];
    searchButton.titleLabel.font = FONT_TWELVE;
    [searchButton addTarget:self action:@selector(search:) forControlEvents:UIControlEventTouchUpInside];
    [searchButton setTitleColor:[UIColor colorWithHex:0x999999] forState:UIControlStateNormal];
    [searchButton setTitle:@"安心100直营店" forState:UIControlStateNormal];
    searchButton.cornerRadius = 5;
    [searchButton setImage:[UIImage imageNamed:@"ss"] forState:UIControlStateNormal];
    self.navigationItem.titleView = searchButton;
    
    
}

#pragma mark - Action

/** 搜索 */
- (void)search:(UIButton *)btu {
   
    LLSearchViewController *vc = [[LLSearchViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

/** 定位 */
- (void)dwAction:(UIButton *)btn {
    
    TLCityPickerController *vc = [TLCityPickerController new];
    vc.commonCitys = [NSMutableArray arrayWithArray:@[@"800220000", @"2800130000", @"700330000"]];
    vc.hotCitys = [NSMutableArray arrayWithArray:@[@"800220000", @"2800130000", @"700330000"]];
    [self.navigationController pushViewController:vc animated:YES];
    
}

/** 客服 */
- (void)kfAction:(UIButton *)btn {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    [self presentViewController:alert animated:YES completion:nil];
    
}





- (XXPageTabView *)pageTabView
{
    if (!_pageTabView) {
        
        AXHomeNcpscVC *vc1 = [[AXHomeNcpscVC  alloc] init];
        
        [self addChildViewController:vc1];
        
        AXHomeNzscVC *vc2 = [[AXHomeNzscVC alloc] init];
        [self addChildViewController:vc2];
        
        AXHomeZxktVC *vc3 = [[AXHomeZxktVC  alloc] init];
        
        [self addChildViewController:vc3];
        
        AXHomeNtsqVC *vc4 = [[ AXHomeNtsqVC alloc] init];
        [self addChildViewController:vc4];
        
        AXHomeZjzdVC *vc5 = [[ AXHomeZjzdVC alloc] init];
        [self addChildViewController:vc5];
        
        
        
        //支持网易云音乐，今日头条，微博等切换栏效果
        _pageTabView = [[XXPageTabView alloc] initWithChildControllers:self.childViewControllers childTitles:@[@"农产品商城",@"农资商城",@"在线课堂",@"农堂社区",@"专家诊断"]];
        _pageTabView.frame = CGRectMake(0, Hn, Wi, Hi-Hn);
        
        _pageTabView.selectedTabIndex = 0;
        _pageTabView.maxNumberOfPageItems = 5;
        _pageTabView.tabSize = CGSizeMake(Wi, 44);
        _pageTabView.tabItemFont = FONT_FOURTEEN;
        /** 适配 */
        if (SCREEN_WIDTH <= 375) {
            
            _pageTabView.tabItemFont = FONT_TWELVE;
        }
        
        _pageTabView.unSelectedColor = White(50);
        _pageTabView.selectedColor = COLOR_GREEN;
        
        _pageTabView.tabBackgroundColor = White(250);
        
        //_pageTabView.separatorColor = [[UIColor grayColor] colorWithAlphaComponent:0.4];
        _pageTabView.bodyBounces = NO;
        
        _pageTabView.indicatorWidth = 0;
        _pageTabView.indicatorHeight = 2;
        
        _pageTabView.delegate = self;
        _pageTabView.titleStyle = XXPageTabTitleStyleDefault;
        _pageTabView.indicatorStyle = XXPageTabIndicatorStyleDefault;
        
        UIScrollView *bodyView = [_pageTabView valueForKey:@"bodyView"];
        [bodyView.panGestureRecognizer requireGestureRecognizerToFail:self.navigationController.interactivePopGestureRecognizer];
    }
    return _pageTabView;
}

- (void)pageTabViewDidEndChange
{
    DLog(@"page = %i", (int)self.pageTabView.selectedTabIndex);
}





@end
